package medium

func scoreOfParentheses(s string) int {
	stack := []int{0}
	pre := ""
	for _, ch := range s {
		if ch == '(' {
			stack = append(stack, 0)
		} else {
			tmp := stack[len(stack)-1]
			stack = stack[:len(stack)-1]
			if pre == "(" {
				tmp += 1
			} else {
				tmp *= 2
			}
			stack[len(stack)-1] = tmp
		}

		pre = string(ch)
	}

	return stack[0]
}
